import { request } from '@umijs/max';
import { message } from 'antd';
import { getIntl } from 'umi';


export async function createProduct(data?: { [key: string]: any }) {
    return request('/admin-api/admin/product/create', {
        method: 'POST',
        data: data
    });
}
export async function updateProduct(data?: { [key: string]: any }) {
    return request('/admin-api/admin/product/update', {
        method: 'POST',
        data: data
    });
}
export async function activeProduct(data?: { [key: string]: any }) {
    return request('/admin-api/admin/product/active', {
        method: 'POST',
        params: data
    });
}

export async function getProductInfoConfig(id:string) {
    let url = `/admin-api/admin/product/info/${id}`;
    return request(url, {
        method: 'GET'
    });
}

export async function deleteProductInfoConfig(id:string) {
    let url = `/admin-api/admin/product/${id}`;
    return request(url, {
        method: 'DELETE'
    });
}


export async function pagedProduct(
    params: {
        current?: number;
        pageSize?: number;
    },
    options?: { [key: string]: any },
) {
    const intl = getIntl(); 
    try {
        const response = await request<API.ApiResponse>('/admin-api/admin/product/page', {
            method: 'GET',
            params: {
                ...params,
            },
            ...(options || {}),
        });

        if (response?.code !== 0) {
            message.error(response?.msg  +  intl.formatMessage({ id: 'request.error.default' }) );
            return [];
        }

        return response?.data || [];
    } catch (error) {
        message.error(intl.formatMessage({ id: 'request.error.network' }) );
        return [];
    }
}


export async function pageListCustomerProducts(params: any): Promise<BackendResponse<any>> {
    let url ='/admin-api/admin/product/distribute/customer/page';
    return request(url, {
        method: 'POST',
        data: params,
    });
}


export async function pageListProductCustomers(params: any): Promise<BackendResponse<any>> {
    let url ='/admin-api/admin/product/distribute/product/page';
    return request(url, {
        method: 'POST',
        data: params,
    });
}

export async function updateProductCustomers(id:string,params: any): Promise<BackendResponse<any>> {
    let url =`/admin-api/admin/product/distribute/product/${id}`;
    return request(url, {
        method: 'PUT',
        data: params,
    });
}

export async function updateCustomerProducts(id:string,params: any): Promise<BackendResponse<any>> {
    let url =`/admin-api/admin/product/distribute/customer/${id}`;
    return request(url, {
        method: 'PUT',
        data: params,
    });
}

export async function listActivatesProducts(params: any): Promise<BackendResponse<any>> {
    let url ='/admin-api/admin/product/list';
    return request(url, {
        method: 'POST',
        data: params,
    });
}
